package com.kgc.ssm.controller;

import com.kgc.ssm.bean.Bill;
import com.kgc.ssm.bean.Provider;
import com.kgc.ssm.constant.CommConstant;
import com.kgc.ssm.service.BillService;
import com.kgc.ssm.service.ProviderService;
import com.kgc.ssm.util.PageSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Controller
public class BillController{

    @Autowired
    private ProviderService providerService;

    @Autowired
    private BillService billService;

    /**
     * @author : zhukang
     * @date   : 2022/8/15
     * @param  : [request, response]
     * @return : void
     * @description : 查询订单列表，带分页
     */
    @RequestMapping( "/billList")
    public String billList(String queryProductName, String queryProviderId, Integer queryIsPayment, Map<String,Object> map,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取查询参数
//        String queryProductName = request.getParameter("queryProductName");
//        String queryProviderId = request.getParameter("queryProviderId");
//        String queryIsPayment = request.getParameter("queryIsPayment") == null ? "0" : request.getParameter("queryIsPayment");

        // 查询所有的供应商列表
        // List<Provider> providers = providerService.getAllProvider();

        // 将供应商列表存入request作用域
        // request.setAttribute("providers", providers);
        map.put("providers",providerService.getAllProvider());

        // 根据查询条件，查询总条数
        int totalCount = billService.getBillCountByCondition(queryProductName, queryProviderId, queryIsPayment);

        // 获取分页参数
        Integer pageNo = request.getParameter("pageNo") == null ? CommConstant.DEFAULT_INIT_PAGE_NO : Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = request.getParameter("pageSize") == null ? CommConstant.DEFAULT_INIT_PAGE_SIZE : Integer.valueOf(request.getParameter("pageSize"));

        // 创建分页对象，封装参数
        PageSupport<List<Bill>> pageSupport = new PageSupport<>();
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);

        // 页码特殊处理
        if(pageNo < 1 || pageSupport.getTotalPage() == 0){
            pageNo = 1;
        } else if(pageNo > pageSupport.getTotalPage()){
            pageNo = pageSupport.getTotalPage();
        }

        // 设置当前页码
        pageSupport.setCurrPageNo(pageNo);

        // 查询所有的订单详情列表，带分页
        List<Bill> billListPage = billService.getBillListByCondition(queryProductName, queryProviderId, queryIsPayment, pageSupport.getCurrPageNo(), pageSupport.getPageSize());

        // 添加分页数据
        pageSupport.setData(billListPage);

        // 将用户列表放到request作用域中
        map.put("pageSupport",pageSupport);

        // 查询条件回显，将原查询条件，放入作用域，页面重新获取
        request.setAttribute("queryProductName", queryProductName);
        request.setAttribute("queryProviderId", queryProviderId);
        request.setAttribute("queryIsPayment", queryIsPayment);

        // 内部转发到用户列表页面
        return "billlist";
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/16
     * @param  : [request, response]
     * @return : void
     * @description : 删除订单详情
     */
    public void billDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取删除订单详情的编号
        String billId = request.getParameter("id");

        // 调用业务接口，根据订单编号，删除订单详情
        billService.delBillById(Integer.valueOf(billId));

        // 重定向到供应商列表页面
        response.sendRedirect("/bills?method=billList");
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 查看订单详情
     */
    public void toViewBill(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取查看的订单详情的编号
        String billId = request.getParameter("id");

        // 调用业务接口，根据订单编号，查询订单详情
        Bill bill = billService.getBillById(Integer.valueOf(billId));

        // 将订单详情对象，存入作用域中
        request.setAttribute("bill", bill);

        // 转发到到订单详情页面
        request.getRequestDispatcher("/jsp/billview.jsp").forward(request, response);
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 跳转修改订单详情页面
     */
    public void toModBill(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取修改的订单详情的编号
        String billId = request.getParameter("id");

        // 调用业务接口，根据订单编号，查询订单详情
        Bill bill = billService.getBillById(Integer.valueOf(billId));

        // 将订单详情对象，存入作用域中
        request.setAttribute("bill", bill);

        // 查询所有的供应商列表
        List<Provider> providers = providerService.getAllProvider();

        // 将供应商列表存入request作用域
        request.setAttribute("providers", providers);

        // 转发到到订单详情页面
        request.getRequestDispatcher("/jsp/billmodify.jsp").forward(request, response);
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 修改订单详情
     */
    public void billMod(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        // 获取修改订单详情的参数
        Bill billForm = new Bill();
        billForm.setId(Integer.valueOf(request.getParameter("bid")));
        billForm.setProductName(request.getParameter("productName"));
        billForm.setProductUnit(request.getParameter("productUnit"));
        billForm.setProductCount(Integer.valueOf(request.getParameter("productCount")));
        billForm.setTotalPrice(Double.valueOf(request.getParameter("totalPrice")));
        billForm.setProviderId(Integer.valueOf(request.getParameter("providerId")));
        billForm.setIsPayment(Integer.valueOf(request.getParameter("isPayment")));
        billForm.setModifyBy((Integer) request.getSession().getAttribute("userId"));

        // 调用业务接口，修改订单详情
        billService.modBillById(billForm);

        // 重定向到供应商列表页面
        response.sendRedirect("/bills?method=billList");
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 跳转添加订单详情页面
     */
    public void toAddBill(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 查询所有的供应商列表
        List<Provider> providers = providerService.getAllProvider();

        // 将供应商列表存入request作用域
        request.setAttribute("providers", providers);

        // 转发到到订单详情页面
        request.getRequestDispatcher("/jsp/billadd.jsp").forward(request, response);
    }

    /**
     * @author : zhukang
     * @date   : 2022/8/12
     * @param  : [request, response]
     * @return : void
     * @description : 增加订单详情
     */
    public void billAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException {
        // 获取增加订单详情的参数
        Bill billForm = new Bill();
        billForm.setBillCode(request.getParameter("billCode"));
        billForm.setProductName(request.getParameter("productName"));
        billForm.setProductUnit(request.getParameter("productUnit"));
        billForm.setProductCount(Integer.valueOf(request.getParameter("productCount")));
        billForm.setTotalPrice(Double.valueOf(request.getParameter("totalPrice")));
        billForm.setProviderId(Integer.valueOf(request.getParameter("providerId")));
        billForm.setIsPayment(Integer.valueOf(request.getParameter("isPayment")));
        billForm.setCreatedBy((Integer) request.getSession().getAttribute("userId"));

        // 调用业务接口，添加供应商详情
        billService.addBillInfo(billForm);

        // 重定向到供应商列表页面
        response.sendRedirect("/bills?method=billList");
    }
}
